package com.cater.talents.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.DemandCommunicate;
import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.talents.entity.RecommendResult;
import com.cater.talents.entity.TalentsRecommend;
import com.cater.talents.service.RecommendResultServiceImpl;
import com.cater.talents.service.TalentsRecommendServiceImpl;

/**
 * 描述：推荐结果控制层
 * 
 * @author yong
 * @date 2019/1/15
 */
@Controller
@RequestMapping("/recommendResult")
public class RecommendResultController {

	@Autowired
	private RecommendResultServiceImpl service;
	@Autowired
	private TalentsRecommendServiceImpl recommendService;
	@Autowired
	private DemandCommunicateServiceImpl communicateService;

	/**
	 * @推荐结果管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "talents/recommendResult/list";
	}

	/**
	 * @推荐结果管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询推荐结果分页数据")
	public PageResult<RecommendResult> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<RecommendResult> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("recommendResult:add")
	public String addView(CommonQuery query, Model model) {
		String recommendId = query.getRecommendId();
		TalentsRecommend recommend = null;
		if (StringUtils.isNotEmpty(recommendId)) {
			recommend = new TalentsRecommend();
			recommend = recommendService.getById(recommendId);
			model.addAttribute("recommendId", recommendId);
		}
		String companyId = query.getCompanyId();
		if (StringUtils.isNotEmpty(companyId)) {
			model.addAttribute("companyId", companyId);
		} else {
			if (recommend != null) {
				model.addAttribute("companyId", recommend.getCompanyId());
			}

		}
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			model.addAttribute("communicateId", communicateId);
		}
		String url = "talents/recommendResult/add";
		String type = query.getType();
		if (StringUtils.isNotEmpty(type)) {
			if (type.equals("1")) {
				url = "talents/workRecord/add";
			}
		}
		return url;
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("recommendResult:add")
	@ResponseBody
	@SystemControllerLog(description = "添加推荐结果")
	public JSONObject add(RecommendResult t, String isPast, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String id = service.saves(t);
			if (StringUtils.isNotBlank(id)) {
				json.put("code", 200);
				json.put("message", "success");
				TalentsRecommend recommend = recommendService.getById(t.getMainId());
				if (recommend != null) {
					recommend.setStatus(t.getStatus().toString());
					recommend.setIsPast(2);
					recommendService.update(recommend);
				}
				String communicateId = query.getCommunicateId();
				if (StringUtils.isNotEmpty(communicateId)) {
					DemandCommunicate communicate = communicateService.getById(communicateId);
					if (communicate != null) {
						communicate.setIsPast(2);
						communicateService.update(communicate);
					}
				}
			} else {
				json.put("code", 400);
				json.put("message", "fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("recommendResult:edit")
	public String editView(String id, Model model, CommonQuery query) {
		RecommendResult t = service.getById(id);
		model.addAttribute("recommendResult", t);
		return "talents/recommendResult/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("recommendResult:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改推荐结果")
	public JSONObject update(RecommendResult t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("recommendResult:delete")
	@SystemControllerLog(description = "删除推荐结果")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("recommendResult:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		RecommendResult t = service.getById(id);
		model.addAttribute("recommendResult", t);
		return "talents/recommendResult/detail";
	}

	@RequestMapping(value = "resultView")
	public String resultView(Model model, CommonQuery query) {
		String recommendId = query.getRecommendId();
		List<RecommendResult> list = null;
		if (StringUtils.isNotEmpty(recommendId)) {
			list = service.getListBy(query);
		}
		model.addAttribute("list", list);
		String url = "talents/recommendResult/resultInfo";
		String type = query.getType();
		if (StringUtils.isNotEmpty(type)) {
			if (type.equals("1")) {
				url = "talents/workRecord/resultInfo";
			}
		}
		return url;
	}

}